<?php

class tx_danplibs_dbosimple {
			
			var $data=array();
			
			/* must be set in childclass*/			
			var $tableName;
			var $keyFields=array();
			
			function _load() {
				if ($this->hasUniqueValues()) {
					$res=$GLOBALS['TYPO3_DB']->exec_SELECTquery(
						'*',
						$this->tableName,
						$this->getUniqueWhere(),
						'',
						'','0,1');		
					if ($res && $GLOBALS['TYPO3_DB']->sql_num_rows($res)>0)	{
						$data=$GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);	
						$this->setDatas($data);
						return true;
					}					
				}
				return false;
			}
			
			
			function edit() {
				if ($this->isDataSecure() && $this->hasUniqueValues()) {
					$update=$this->_getSecuredDataArrayForSQL();					
					$GLOBALS['TYPO3_DB']->exec_UPDATEquery($this->tableName,$this->getUniqueWhere(),$update);	
				}			
			}
			
			function setDatas($array) {
				foreach ($array as $k=>$v) {
					$this->setData($k,$v);				
				}
			}
			function unsetAllData() {				
				$this->data=array();
			}
			
			function setData($k,$v) {				
				$this->data[$k]=$v;								
			}
			function unsetData($k) {				
				unset($this->data[$k]);
			}
			
			function getField($k) {
					return $this->data[$k];				
			}
			
			function insert() {				
				if ($this->isDataSecure()) {
					$insert=$this->_getSecuredDataArrayForSQL();
					$insert=$this->modifyForInsert($insert);						
					$GLOBALS['TYPO3_DB']->exec_INSERTquery($this->tableName,$insert);						
				}
				
			}
			
			function _getSecuredDataArrayForSQL() {
				
				/*
				foreach ($this->data as $k=>$v) {
						$insert[$k]=$this->quoteStr($v);
				}
				*/				
				return $this->data;
				
			}			
			
			/*
				use it to unset the keys
			*/
			function modifyForInsert($insert) {
				foreach ($this->keyFields as $v) {
					unset($insert[$k]);
				}
				return $insert;
			}
			/*
				use $this->data to return the where clause e.g. uid=$this->data['uid']				
				Security check is allready done before this function is called				
			*/
			function getUniqueWhere() {
				foreach ($this->keyFields as $v) {
					$where.=$AND.$v.'=\''.$this->getField($v).'\'';
					$AND=' AND ';
				}
				return $where;
			}
			/* abstrak functions */
			//************************			
			
			/* 
			 e.g. check if keys are numeric
			 called before insert() edit() and optional in your load() function
			 @returns true or false
			*/
			function isDataSecure() {
				return true;
			}
			
			
			function hasUniqueValues() {
				foreach ($this->keyFields as $v) {
					if ($this->getField($v)=='')
						return false;
				}
				return true;
			}
			
			/*
			load needs some parameters sample function:
			***************************
			function load($key1,$key2) {
				$data=array('key1'=>$key1,'key2'=>$key2);	
				$this->setDatas($data);
				if ($this->isDataSecure()) {				
					return $this->_load();
				}
				else {
					$this->unsetAllData();
					return false;
				}					
			}
			*/
			
}
	
?>